package com.spboot.redis.contoller;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiOperation;

@RestController
public class RedisController {

	//springboot redis api
	@Autowired
	StringRedisTemplate redisTemplate;
	
	public void ex(String key){
		//对所有数据类型进行单独失效设置
		redisTemplate.expire(key, 1000, TimeUnit.MILLISECONDS);
	}
	
	@ApiOperation(value = "查看redis中所有的key")
	@GetMapping("/keys") //一切方法都可以 get post .... 限制请求方法
	public Set<String> keys(){
		return redisTemplate.keys("*");
	}

	@ApiOperation(value = "删除redis中所有的key")
	@GetMapping("/delAll")
	public Long delAll(){
		return redisTemplate.delete(redisTemplate.keys("*"));
	}

	@ApiOperation(value = "添加字符串数据类型")
	//String
	@PostMapping("/strSet")
	public void strSet(String key,String value){
		//1.先选择要操作的数据类型 使用opsForXXX方法
		//针对字符串类型 失效 时长,单位(秒 毫秒 分钟...)
		redisTemplate.opsForValue().set(key, value,30,TimeUnit.MINUTES);
	}

	@ApiOperation(value = "通过key获取字符串")
	@PostMapping("/strGet")
	public String strGet(String key){
		return redisTemplate.opsForValue().get(key);
	}

	//List
	@PostMapping("/rPush")
	public Long rPush(String key,String [] value){
//		redisTemplate.opsForList().leftPush(key, value);//添加到最前面
		return redisTemplate.opsForList().rightPushAll(key,value);//按顺序添加
	}

	@PostMapping("/range")
	public List<String> range(String key){
		//集合长度
		Long size = redisTemplate.opsForList().size(key);
		return redisTemplate.opsForList().range(key, 0, size);
	}
	
	//hash
	@PostMapping("/put")
	public void put(String key,String hashKey,String value){
		redisTemplate.opsForHash().put(key, hashKey, value);
	}

	@PostMapping("/hget")
	public Map<Object, Object> get(String key){
		redisTemplate.opsForHash().keys(key);
		redisTemplate.opsForHash().values(key);
		//返回map 包含key 和value
		return redisTemplate.opsForHash().entries(key);
	}
	
	//set
	@PostMapping("/sadd")
	public Long sadd(String key,String [] value){
		return redisTemplate.opsForSet().add(key, value);
	}
	
	@PostMapping("/members")
	public Set<String> members(String key){
		return redisTemplate.opsForSet().members(key);
	}

	@PostMapping("/zadd")
	public Boolean zadd(String key,String value,Double score){
		return redisTemplate.opsForZSet().add(key, value, score);
	}

	@PostMapping("/zrange")
	public Set<TypedTuple<String>> zrange(String key){
		Long size = redisTemplate.opsForZSet().size(key);
		//从小到大
//		return redisTemplate.opsForZSet().range(key, 0, size);
		//从小到大 带score的排序
//		return redisTemplate.opsForZSet().rangeWithScores(key, 0, size);
		//从大到小 带score的排序
		return redisTemplate.opsForZSet().reverseRangeWithScores(key, 0, size);
	}
}
